package com.ruoyi.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZhwyShops;
import com.ruoyi.system.service.IZhwyShopsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商铺信息管理Controller
 * 
 * @author nhh
 * @date 2024-05-04
 */
@RestController
@RequestMapping("/system/shops")
public class ZhwyShopsController extends BaseController
{
    @Autowired
    private IZhwyShopsService zhwyShopsService;

    // @PreAuthorize("@ss.hasPermi('system:shops:list')")
    @Anonymous
    @GetMapping("/floor/{id}")
    public AjaxResult list(@PathVariable Long id){
        return success(zhwyShopsService.findByBuildingId(id));
    }
    @Anonymous
    @GetMapping("/changestate/{id}")
    public AjaxResult changeState(@PathVariable Long id){
//        改变商铺出租状态，暂定从空置转为出租，后续和扩展不同业务状态
        int state = 1;
        return success(zhwyShopsService.changeStateById(id,state));
    }

    /**
     * 查询商铺信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:shops:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZhwyShops zhwyShops)
    {
        startPage();
        List<ZhwyShops> list = zhwyShopsService.selectZhwyShopsList(zhwyShops);
        return getDataTable(list);
    }

    /**
     * 导出商铺信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:shops:export')")
    @Log(title = "商铺信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhwyShops zhwyShops)
    {
        List<ZhwyShops> list = zhwyShopsService.selectZhwyShopsList(zhwyShops);
        ExcelUtil<ZhwyShops> util = new ExcelUtil<ZhwyShops>(ZhwyShops.class);
        util.exportExcel(response, list, "商铺信息管理数据");
    }

    /**
     * 获取商铺信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:shops:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zhwyShopsService.selectZhwyShopsById(id));
    }

    /**
     * 新增商铺信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:shops:add')")
    @Log(title = "商铺信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZhwyShops zhwyShops)
    {
        return toAjax(zhwyShopsService.insertZhwyShops(zhwyShops));
    }

    /**
     * 修改商铺信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:shops:edit')")
    @Log(title = "商铺信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZhwyShops zhwyShops)
    {
        return toAjax(zhwyShopsService.updateZhwyShops(zhwyShops));
    }

    /**
     * 删除商铺信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:shops:remove')")
    @Log(title = "商铺信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zhwyShopsService.deleteZhwyShopsByIds(ids));
    }
//逻辑删除
    @PreAuthorize("@ss.hasPermi('system:shops:remove')")
    @Log(title = "商铺信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{ids}")
    public AjaxResult del(@PathVariable Long[] ids)
    {
        return toAjax(zhwyShopsService.del(ids));
    }

    @PreAuthorize("@ss.hasPermi('system:shops:remove')")
    @Log(title = "商铺信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/canceldel/{id}")
    public AjaxResult canceldel(@PathVariable Long id)
    {
        return toAjax(zhwyShopsService.canceldel(id));
    }


    /**
     * 获取房屋租售数据
     */
    @Anonymous
    @GetMapping("/getHome")
    public AjaxResult getHome(){
        List<ZhwyShops> list = zhwyShopsService.selectZhwyShopsList(null);
        Integer cs = 0;
        Integer cz = 0;
        Integer kz = 0;
        for (ZhwyShops zhwyShops : list){
            if(zhwyShops.getState() == 0){
                kz ++;
            }
            if(zhwyShops.getState() == 1){
                cz ++;
            }
            if(zhwyShops.getState() == 2){
                cs ++;
            }
        }
        List<Integer> list1 = new ArrayList<>();
        list1.add(kz);
        list1.add(cz);
        list1.add(cs);
        return AjaxResult.success(list1);
    }

}
